package net.guerlab.smart.platform.user.web.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.guerlab.commons.exception.ApplicationException;
import net.guerlab.smart.platform.excel.ExcelUtils;
import net.guerlab.smart.platform.server.controller.BaseFindController;
import net.guerlab.smart.platform.user.auth.UserContextHandler;
import net.guerlab.smart.platform.user.core.domain.LoginLogDTO;
import net.guerlab.smart.platform.user.core.exception.LoginLogInvalidException;
import net.guerlab.smart.platform.user.core.searchparams.LoginLogSearchParams;
import net.guerlab.smart.platform.user.service.entity.LoginLog;
import net.guerlab.smart.platform.user.service.service.LoginLogService;
import net.guerlab.smart.platform.user.service.service.OperationLogHandler;
import net.guerlab.smart.platform.user.web.excel.LoginLogExcelExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 登录记录
 *
 * @author guer
 */
@Api(tags = "登录记录")
@RestController("/user/loginLog")
@RequestMapping("/user/loginLog")
public class LoginLogController
        extends BaseFindController<LoginLogDTO, LoginLog, LoginLogService, LoginLogSearchParams, Long> {

    private OperationLogHandler operationLogHandler;

    @Override
    protected ApplicationException nullPointException() {
        return new LoginLogInvalidException();
    }

    @ApiOperation("导出Excel")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, LoginLogSearchParams searchParams) {
        beforeFind(searchParams);
        ExcelUtils.exportExcel(response, getService().selectAll(searchParams), LoginLogExcelExport.class,
                "LoginLog-" + System.currentTimeMillis());
        operationLogHandler.add("导出登录记录列表excel", UserContextHandler.getUserId(), searchParams);
    }

    @Autowired
    public void setOperationLogHandler(OperationLogHandler operationLogHandler) {
        this.operationLogHandler = operationLogHandler;
    }
}
